package cn.italkcloud.cph.service.impl;

import cn.italkcloud.cph.entity.VosServer;
import cn.italkcloud.cph.mapper.VosServerMapper;
import cn.italkcloud.cph.service.IVosServerService;
import cn.italkcloud.cph.utils.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * <p>
 * VOS服务器 服务实现类
 * </p>
 *
 * @author dominate
 * @since 2025-03-07
 */
@Service
public class VosServerServiceImpl extends ServiceImpl<VosServerMapper, VosServer> implements IVosServerService {

    @Override
    public boolean isOwner(int accountId, int id) {
        QueryWrapper<VosServer> query = new QueryWrapper<>();
        query.lambda().eq(VosServer::getAccountId, accountId)
                .eq(VosServer::getId, id);
        return count(query) > 0;
    }

    @Override
    public List<VosServer> query(int accountId, String name, boolean forList) {
        QueryWrapper<VosServer> query = new QueryWrapper<>();
        query.lambda().eq(0 != accountId, VosServer::getAccountId, accountId)
                .like(StringUtil.isNotBlank(name), VosServer::getName, name);
        if (forList) {
            query.lambda().select(VosServer::getId, VosServer::getName, VosServer::getAccountId);
        }
        return list(query);
    }

    @Override
    public String getApiUrl(Integer id) {
        if (Objects.isNull(id)) {
            return StringUtil.EMPTY;
        }
        QueryWrapper<VosServer> query = new QueryWrapper<>();
        query.lambda().eq(VosServer::getId, id)
                .select(VosServer::getApiUrl);
        if (count(query) == 0) {
            return StringUtil.EMPTY;
        }
        return getOne(query).getApiUrl();
    }

    @Override
    public Map<Integer, String> getApiUrlMap(Collection<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyMap();
        }
        QueryWrapper<VosServer> query = new QueryWrapper<>();
        query.lambda().in(VosServer::getId, ids)
                .select(VosServer::getApiUrl, VosServer::getId);
        return queryMap(query, false);
    }

    @Override
    public Map<Integer, String> getApiUrlMap() {
        QueryWrapper<VosServer> query = new QueryWrapper<>();
        query.lambda().select(VosServer::getApiUrl, VosServer::getId);
        return queryMap(query, false);
    }

    @Override
    public List<VosServer> getValid() {
        QueryWrapper<VosServer> query = new QueryWrapper<>();
        return list(query);
    }

    @Override
    public Map<Integer, String> getNameMap(Collection<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyMap();
        }
        QueryWrapper<VosServer> query = new QueryWrapper<>();
        query.lambda().in(VosServer::getId, ids)
                .select(VosServer::getName, VosServer::getId);
        return queryMap(query, true);
    }

    private Map<Integer, String> queryMap(QueryWrapper<VosServer> query, boolean getName) {
        List<VosServer> list = list(query);
        Map<Integer, String> map = new HashMap<>(list.size());
        for (VosServer vosServer : list) {
            map.put(vosServer.getId(), getName ? vosServer.getName() : vosServer.getApiUrl());
        }
        return map;
    }
}
